package cn.new7as.cas5s.tool.phut.funs;

import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorManager
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.wifi.WifiManager
import android.os.Build
import android.os.SystemClock
import android.provider.Settings
import android.telephony.*
import android.util.Log
import cn.new7as.cas5s.tool.phut.Hkxadfd
import cn.new7as.cas5s.tool.phut.Kchgaudf
import cn.new7as.cas5s.tool.phut.NMhdj3hj
import cn.new7as.cas5s.tool.phut.Nhxjkasaf3dd
import cn.new7as.cas5s.tool.util.PhoneDataUtil
import cn.new7as.cas5s.tool.util.getOtherData

import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.FlashlightUtils
import com.blankj.utilcode.util.NetworkUtils
import com.blankj.utilcode.util.SPUtils
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.google.gson.Gson

import kotlinx.coroutines.runBlocking
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.net.InetAddress
import java.net.NetworkInterface
import java.util.*

object GerPhoneDataFuntion {
    var phoneData: PhoneData? = PhoneData();


    fun getDeviceData(context: Activity): PhoneData? {
        var phoneData: PhoneData?
        runBlocking<Unit> {
            phoneData = PhoneDataUtil.getInstance().phonedata()
        }
        this.phoneData = phoneData

        try {
            getSensorList(context)

        } catch (e: Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)
        }
        try {
            getPhoneScreenInfo(context)

        } catch (e: Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)
        }
        try {
            phoneData?.kpcLaqiHcqVwmKsj = getOtherData()

        } catch (e: Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)

        }
        try {
            getPhoneInfoHard()

        } catch (e: Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)
        }
        try {
            getDeviceOther(context)

        } catch (e: Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)
        }
        try {
            getSimulatorInfo(context)

        } catch (e: Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)
        }
        try {
            getLocalInfo(context)

        } catch (e: Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)
        }
        return phoneData
    }


    fun getSimulatorInfo(context: Context) {
        var NMhdj3hj = NMhdj3hj()
        try {
            NMhdj3hj.wxsxpVdpddFhoib = Build.CPU_ABI
            NMhdj3hj.plymrQrgkBbcx = Build.CPU_ABI2
            NMhdj3hj.ixkmPtfluHmigQwvr = Build.TAGS
            NMhdj3hj.oensVeaPusdq = Build.USER
            NMhdj3hj.kmyxqNwamEwhoVyyln = Build.TYPE
            NMhdj3hj.wgjNdzDjevmTyq = Build.BOARD
            NMhdj3hj.rugikQhnCvmkPfxfgGea = Build.BRAND
            NMhdj3hj.ykgQpoPwmfXsv = Build.PRODUCT
            NMhdj3hj.fxrgdFinmcQpyjfIqmchQob = Build.MANUFACTURER
            NMhdj3hj.hkbvkIzrDelaZeau = FlashlightUtils.isFlashlightEnable()
            NMhdj3hj.fnoLrglToeh = DeviceUtils.isEmulator()

            phoneData?.spcIkoNjaoxWjw = NMhdj3hj
        } catch (e: Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)
        }
    }


    fun getSensorList(context: Context) {
        val jsonArray = JSONArray()

        val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager

        val sensors = sensorManager.getSensorList(Sensor.TYPE_ALL)
        val Hgxkja = Hgxkja("");
        try {
            for (item in sensors) {
                val jsonObject = JSONObject()
                jsonObject.put("type", item.type.toString())
                jsonObject.put("name", item.name)
                jsonObject.put("version", item.version.toString())
                jsonObject.put("vendor", item.vendor)
                jsonObject.put("maxRange", item.maximumRange.toString())
                jsonObject.put("minDelay", item.minDelay.toString())
                jsonObject.put("power", item.power.toString())
                jsonObject.put("resolution", item.resolution.toString())
                jsonArray.put(jsonObject)
            }
            Hgxkja.cfaMgfEgruXij = jsonArray.toString()
            phoneData?.thkrUdhFntQuncvDdsoe = Hgxkja
        } catch (e: java.lang.Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)
        }
        return
    }


    fun getDeviceOther(context: Context) {
        var Nhxjkasaf3dd = Nhxjkasaf3dd()
        phoneData?.rjtsyOyqMmoqDdw = Nhxjkasaf3dd
        try {
            Nhxjkasaf3dd.ysclUixZiway = Build.MODEL;
            val telephonyManager =
                context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager?

            val wifiManager: WifiManager =
                context.getSystemService(Context.WIFI_SERVICE) as WifiManager
            Nhxjkasaf3dd.zfgxlBxxhbHmr = wifiManager?.connectionInfo?.macAddress;



            Nhxjkasaf3dd.zsymFjnRecnLwxjr = Build.VERSION.RELEASE;

            val displayMetrics = context.resources.displayMetrics
            //  deviceOtherInfo.screen_size = intArrayOf(displayMetrics.widthPixels, displayMetrics.heightPixels).toString();
            //  deviceOtherInfo.wifi = wifiManager?.connectionInfo?.ssid
            //  deviceOtherInfo.wifi = FuntionUtil.getWifiList()
            Nhxjkasaf3dd.tupeLfnMvhy = Build.getRadioVersion()
            Nhxjkasaf3dd.gnbfhAycNcprZlo = System.getProperty("os.version")
            Nhxjkasaf3dd.zmsHmxevDvfkFhcrb = Build.CPU_ABI
            Nhxjkasaf3dd.wwdtFeqxpOohyaJzgIno = 1
            Nhxjkasaf3dd.xxxNzkdzDet = 0
            Nhxjkasaf3dd.qpmNsgDlbucGxdeLha = "android"
            Nhxjkasaf3dd.cnaoCzhfaQzrbcNmsl = NetworkUtils.getNetworkType().toString();
            Nhxjkasaf3dd.xkivmLueVfvHve = getIPAddress()
            Nhxjkasaf3dd.sbvsgLmcbtTtlnpXawmTynw = getIPAddress()
            Nhxjkasaf3dd.wexnJqlgKougx = Build.VERSION.RELEASE
            Nhxjkasaf3dd.lkrJaiYwxScekm = Build.VERSION.RELEASE
            Nhxjkasaf3dd.ucmsqZyyiuWwzlMsuu =
                Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            Nhxjkasaf3dd.doiPcrKmwGpkdrNsix =
                Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            Nhxjkasaf3dd.pfrXenqdApxrKgesb =
                Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            Nhxjkasaf3dd.pnywxDpzBirw =
                Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            //   deviceOtherInfo.information = getBrowserList2(context);
            Nhxjkasaf3dd.yurfuZhniMxavf = getSignalStrength(context)
            Nhxjkasaf3dd.lhuxQmfbMndpq = wifiManager?.connectionInfo

            Nhxjkasaf3dd.eyyjmVvyOplXuet = wifiManager?.connectionInfo?.bssid
            val uptimeMillis: Long = SystemClock.uptimeMillis()
            val currentTimeMillis = System.currentTimeMillis()
            var lastBootTimeMillis = currentTimeMillis - uptimeMillis
            Nhxjkasaf3dd.vqxePgiEphroBbc = lastBootTimeMillis.toString()
            Nhxjkasaf3dd.fchUkzgYevDdchaTqu = lastBootTimeMillis.toString()
            Nhxjkasaf3dd.czowBjwymEaqBsxvy = SystemClock.elapsedRealtime().toString()

            Nhxjkasaf3dd.uamxcZaequGskcOdcz = wifiManager?.connectionInfo?.macAddress;
            Nhxjkasaf3dd.dgdtPamoLjdcaYwuwm = if (isVpnConnected(context)) 1 else 2
            Nhxjkasaf3dd.shpRxhSyygKgmt = if (isProxyEnabled()) 1 else 2
            Nhxjkasaf3dd.wwdtFeqxpOohyaJzgIno = 1

            try {
                val advertisingIdInfo: AdvertisingIdClient.Info =
                    AdvertisingIdClient.getAdvertisingIdInfo(context.getApplicationContext());
                Nhxjkasaf3dd.gwouyTjkkeKarJyqh = advertisingIdInfo.getId();

            } catch (e: Exception) {
                phoneData?.zxaRbdeBybxi!!.add(e)
            }
            Nhxjkasaf3dd.zfezQsxvpGgfxvEsqz = if (isSuEnable()) 1 else 0

            val bluetoothAdapter: BluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
            val pairedDevices: Set<BluetoothDevice> = bluetoothAdapter.getBondedDevices()
            val numberOfPairedDevices = pairedDevices.size
            Nhxjkasaf3dd.lafcLugWajsyUzsoy = numberOfPairedDevices.toString()


            Nhxjkasaf3dd.cfpfLgqDdkijWtkr = telephonyManager?.getLine1Number();
            Nhxjkasaf3dd.nwagAorqqMgpwi = telephonyManager?.getLine1Number();
            try {
            } catch (e: Exception) {

            }


            for (slot in 0 until telephonyManager!!.phoneCount) {
                if (slot == 0) Nhxjkasaf3dd.bikvEoigCyxsvWjfdnPyv =
                    telephonyManager!!.getDeviceId(slot)

                if (slot == 1) Nhxjkasaf3dd.agsneLvdUnn = telephonyManager!!.getDeviceId(slot)

            }


        } catch (e: Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)
        }


        return
    }


    fun getPhoneScreenInfo(context: Context) {
        val dm = context.getResources().getDisplayMetrics()
        var hkxadfd = Hkxadfd();
        try {
            val screenJson: String = Gson().toJson(dm)
            val jsonObject = JSONObject(screenJson)
            hkxadfd.mteypEhaeFxbhJkvx = jsonObject.getInt("noncompatHeightPixels")
            hkxadfd.khsbkQkozdVwtur = jsonObject.getInt("noncompatDensityDpi")
            hkxadfd.jhyxnLnivqLvplgUebwWrb = jsonObject.getInt("noncompatWidthPixels")

            val noncompatDensity = jsonObject.getInt("noncompatDensity")
            hkxadfd.mhjwRiyYrptMsk = noncompatDensity
            val noncompatScaledDensity = jsonObject.getDouble("noncompatScaledDensity")
            hkxadfd.cwfXxorGmy = noncompatScaledDensity
            val noncompatXdpi = jsonObject.getDouble("noncompatXdpi")
            //  phoneScreenInfo.noncompatXdpi = noncompatXdpi
            val noncompatYdpi = jsonObject.getDouble("noncompatYdpi")

        } catch (e: Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)
        }

        hkxadfd.aqffnCmcmaSwrqqTvh = dm.xdpi;
        hkxadfd.lrkSugcHmeAskdCgq = dm.ydpi;
        hkxadfd.jkyYzlSutp = dm.density
        hkxadfd.xasyDeolWociq = dm.densityDpi
        hkxadfd.fdjbKgjGwua = dm.heightPixels
        hkxadfd.irqDtuvdIzwUwlGmufl = dm.widthPixels
        hkxadfd.aikxlKtawGvs = dm.widthPixels.toString().plus("*").plus(dm.heightPixels)
        hkxadfd.kpbkwWfmskCempfFflaw =
            dm.widthPixels.toString().plus("*").plus(dm.heightPixels)
        hkxadfd.kpbwtRtyoKhsgdDeldf = dm.scaledDensity
        hkxadfd.wtmcvClnxEuighKcgot = dm.xdpi
        hkxadfd.atvaDxvhbFqufmLoqqs = dm.ydpi
        phoneData?.fpfCyqfuXyfFzhwx = hkxadfd
        return
    }

    fun getPhoneInfoHard() {
        val njx3hjs = Njx3hjs()
        njx3hjs.rugikQhnCvmkPfxfgGea = Build.BOARD
        njx3hjs.vsnZonsLalPlpn = Build.BRAND
        njx3hjs.ezhRmvIwpRtqna = Build.DEVICE
        njx3hjs.oxvbaTnqQceSsel = Build.HARDWARE
        njx3hjs.bktpeXtdXgu = Build.MODEL
        njx3hjs.whjrwOvqplYtxBknJhiz = Build.PRODUCT
        njx3hjs.bqgrKrbcJuj = Build.MANUFACTURER
        njx3hjs.ypeZyycFpddz = Build.FINGERPRINT
        njx3hjs.emelcIbcNpbck = Build.DISPLAY
        njx3hjs.RadioVersion = Build.getRadioVersion()
        njx3hjs.sxtgYnwrjIrfmyJbru = Build.SERIAL
        njx3hjs.zwqqzHvgsdSviGhpnw = Build.HOST
        njx3hjs.wltjIrndWawjtOci = Build.ID
        njx3hjs.uqyBwhylVizIsc = Build.VERSION.RELEASE
        njx3hjs.waztgTvcvUjctXlmat = Build.BRAND
        phoneData?.attEwylnHllkAqm = njx3hjs
        return
    }

    fun getLocalInfo(context: Context) {
        var kchgaudf = Kchgaudf()
        try {


            val gson = Gson()
            if (SPUtils.getInstance().getString("loposition") != null)
                kchgaudf =
                    gson.fromJson(
                        SPUtils.getInstance().getString("loposition"),
                        Kchgaudf::class.java
                    )

        } catch (ex: java.lang.Exception) {
            phoneData?.zxaRbdeBybxi!!.add(ex)
        }
        phoneData?.sryHkxfNzaQyhcx = kchgaudf
        return
    }

    private fun isVpnConnected(context: Context): Boolean {
        val connectivityManager: ConnectivityManager? =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
        if (connectivityManager != null) {
            for (network in connectivityManager.getAllNetworks()) {
                val capabilities: NetworkCapabilities? =
                    connectivityManager.getNetworkCapabilities(network)
                if (capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
                    return true
                }
            }
        }
        return false
    }

    private fun isProxyEnabled(): Boolean {
        val proxyHost = System.getProperty("http.proxyHost")
        val portStr = System.getProperty("http.proxyPort")
        val proxyPort = (portStr ?: "-1").toInt()
        return proxyHost != null && proxyPort != -1
    }

    fun getIPAddress(): String? {
        var ipAddress = ""
        try {
            val interfaces: List<NetworkInterface> =
                Collections.list(NetworkInterface.getNetworkInterfaces())
            for (intf in interfaces) {
                val addrs: List<InetAddress> = Collections.list(intf.getInetAddresses())
                for (addr in addrs) {
                    if (!addr.isLoopbackAddress()) {
                        ipAddress = addr.getHostAddress()
                    }
                }
            }
        } catch (ex: java.lang.Exception) {
            Log.e("IP Address", ex.toString())
            phoneData?.zxaRbdeBybxi!!.add(ex)

        }
        return ipAddress
    }


    private fun getSignalStrength(context: Context): Int {
        val telephonyManager =
            context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager?

        try {

            val cellInfo: CellInfo = telephonyManager!!.allCellInfo[0]
            return if (cellInfo is CellInfoGsm) {
                val signalStrengthGsm: CellSignalStrengthGsm =
                    (cellInfo as CellInfoGsm).getCellSignalStrength()
                signalStrengthGsm.getDbm()
            } else if (cellInfo is CellInfoCdma) {
                val signalStrengthCdma: CellSignalStrengthCdma =
                    (cellInfo as CellInfoCdma).getCellSignalStrength()
                signalStrengthCdma.getDbm()
            } else if (cellInfo is CellInfoLte) {
                val signalStrengthLte: CellSignalStrengthLte =
                    (cellInfo as CellInfoLte).getCellSignalStrength()
                signalStrengthLte.getDbm()
            } else if (cellInfo is CellInfoWcdma) {
                val signalStrengthWcdma: CellSignalStrengthWcdma =
                    (cellInfo as CellInfoWcdma).getCellSignalStrength()
                signalStrengthWcdma.getDbm()
            } else {
                0
            }
        } catch (e: Exception) {
            phoneData?.zxaRbdeBybxi!!.add(e)

            return 0
        }
    }


    fun isSuEnable(): Boolean {

        var file: File? = null
        val paths = arrayOf(
            "/system/bin/",
            "/system/xbin/",
            "/system/sbin/",
            "/sbin/",
            "/vendor/bin/",
            "/su/bin/"
        )
        try {
            for (path in paths) {
                file = File(path + "su")
                if (file.exists() && file.canExecute()) {

                    return true
                }
            }
        } catch (x: java.lang.Exception) {
            x.printStackTrace()
            phoneData?.zxaRbdeBybxi!!.add(x)

        }
        return false
    }


}
